package com.funmi.lizip.security.controller;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyIndexController {
    @GetMapping("/index")
    public String index() {
        return "Hello World ~";
    }

    //支持校验角色，角色不用加上“ROLE_”
    //@PreAuthorize("hasRole('admin')")
    // 角色中需要加上“ROLE_”
    //@Secured({"ROLE_admin"})
    // 自定义权限检查规则
    //@PreAuthorize("@pms.hasPermission(\"ROLE_admin\")")
    @RequestMapping("/whoim")
    public Object whoIm() {
        SecurityContext context = SecurityContextHolder.getContext();
        Authentication authentication = context.getAuthentication();
        Object principal = authentication.getPrincipal();

        return principal;
    }
}